﻿using AntdUI;
using S7SingalTool.Controls.SubView;
using S7SingalTool.Forms;
using S7SingalTool.Models.Table;
using S7SingalTool.Utils;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace S7SingalTool.Controls
{
    public partial class PlcBlockPage : BaseUserControl
    {
        public PlcBlockPage()
        {
            InitializeComponent();
            InitTable();
            this.Load += PlcBlockPage_Load;
            txtSearch.TextChanged += TxtSearch_TextChanged;
        }

       

        private void PlcBlockPage_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {


            BlockDbEditForm form = new BlockDbEditForm();
            form.ShowDialog();
            LoadData();


        }

        private void InitTable()
        {
            table1.EditMode = TEditMode.None;
            table1.Columns = new ColumnCollection();
            //  table1.Columns.Add(new ColumnCheck("IsCheck") { });
            // table1.Columns.Add(new Column("Id", "ID"));
           // table1.Columns.Add(new Column("WriteFromCellText", "写入方"));
            table1.Columns.Add(new Column("WorkstationCode", "工站编号") { });
            table1.Columns.Add(new Column("BlockDb", "数据块"));
          //  table1.Columns.Add(new Column("ModuleCellText", "模块"));

            table1.Columns.Add(new Column("Operate", "操作") { Fixed = true });
            table1.Bordered = true;
            table1.EnableHeaderResizing = true;
            table1.EmptyHeader = true;
            table1.Gap = 6;

            //table1.CellBeginEditInputStyle += Table1_CellBeginEditInputStyle;
            //table1.CellEndEdit += Table1_CellEndEdit;
            table1.CellButtonClick += Table1_CellButtonClick;
            table1.CellClick += Table1_CellClick;
        }

        private void Table1_CellClick(object sender, TableClickEventArgs e)
        {
            var col = table1.Columns[e.ColumnIndex];
            if (col.Key == "BlockDb")
            {
                table1.EnterEditMode(e.RowIndex, e.ColumnIndex);
            }
        }

        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var item = e.Record as PlcBlockDb;
           
            switch (e.Btn.Id)
            {
                case "save":
                    if (item.Id < 1)
                    {
                        AntdUI.Message.error(App.MainLayout, "Id不能小于1", autoClose: 2);
                        return;
                    }
                    var Exists = fsql.Queryable<PlcBlockDb>().Where(a => a.Id == item.Id).Any();
                    if (Exists)
                    {
                        fsql.Update<PlcBlockDb>().SetSource(item).Where(a => a.Id == item.Id).ExecuteAffrows();

                        fsql.Update<CksPlcDataPosition>()
                            .Set(a => a.BlockDb == item.BlockDb)
                            .Where(a => a.BlockDbId == item.Id)
                            .ExecuteAffrows();


                    }
                    else
                    {
                        fsql.Insert(item).ExecuteAffrows();
                    }
                    AntdUI.Message.info(App.MainLayout, "保存成功", autoClose: 1);
                    break;
                case "del":
                    var result = Modal.open(this.FindForm(), "删除警告！", "确认要删除选择的数据吗，同时删除该DB块下的点位数据？", TType.Warn);
                    if (result == DialogResult.OK)
                    {
                        fsql.Delete<PlcBlockDb>().Where(a => a.Id == item.Id).ExecuteAffrows();
                        fsql.Delete<CksPlcDataPosition>()
                            .Where(a=>a.PlcId == item.PlcId && a.BlockDbId == item.Id)
                            .ExecuteAffrows();
                        Source.Remove(item);
                    }
                    break;
                default:
                    break;
            }
        }

        BindingList<PlcBlockDb> Source = new BindingList<PlcBlockDb>();
        private void LoadData()
        {
            string search = txtSearch.Text;
            Source.Clear();

            var plcs = fsql.Select<PlcConfig>().WhereIf(!search.IsNullOrEmpty(), (a) => a.WorkstationCode.Contains(search))
                .ToList();
           
            var pbd = fsql.Select <PlcBlockDb>().ToList();
            var lst = new List<PlcBlockDb>();
            foreach (var item in plcs)
            {
                var ones = pbd.FindAll(a => a.PlcId == item.Id);
                foreach (var one in ones)
                {
                    one.WorkstationCode = item.WorkstationCode;
                    lst.Add(one);
                }
                
            }
            foreach (var item in lst)
            {
                Source.Add(item);
            }
            this.table1.Binding(Source);
        }

        private void TxtSearch_TextChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
